#include "stdafx.h"
#include "UIButton.h"

namespace DuiLib
{
	CButtonUI::CButtonUI()
		: m_uButtonState(0)
		, m_dwHotTextColor(0)
		, m_dwPushedTextColor(0)
		, m_dwFocusedTextColor(0)
		, m_dwPushedBackColor(0)
		, m_dwPushedBackColor2(0)
		, m_dwPushedBackColor3(0)
		, m_dwHotBackColor(0)
		, m_dwHotBackColor2(0)
		, m_dwHotBackColor3(0)
		, m_dwFocusedBackColor(0)
		, m_dwFocusedBackColor2(0)
		, m_dwFocusedBackColor3(0)
		, m_dwFocusedHotBackColor(0)
		, m_dwFocusedHotBackColor2(0)
		, m_dwFocusedHotBackColor3(0)
		, m_dwHotBorderColor(0)
		, m_dwPushedBorderColor(0)
		, m_dwFocusedBorderColor(0)
	{
		m_uTextStyle = DT_SINGLELINE | DT_VCENTER | DT_CENTER;
	}

	LPCTSTR CButtonUI::GetClass() const
	{
		return _T("ButtonUI");
	}

	LPVOID CButtonUI::GetInterface(LPCTSTR pstrName)
	{
		if( _tcscmp(pstrName, DUI_CTR_BUTTON) == 0 ) return static_cast<CButtonUI*>(this);
		return CLabelUI::GetInterface(pstrName);
	}

	UINT CButtonUI::GetControlFlags() const
	{
		return (IsKeyboardEnabled() ? UIFLAG_TABSTOP : 0) | (IsEnabled() ? UIFLAG_SETCURSOR : 0);
	}

	void CButtonUI::DoEvent(TEventUI& event)
	{
		if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
			if( m_pParent != NULL ) m_pParent->DoEvent(event);
			else CLabelUI::DoEvent(event);
			return;
		}

		if( event.Type == UIEVENT_SETFOCUS ) 
		{
			Invalidate();
		}
		if( event.Type == UIEVENT_KILLFOCUS ) 
		{
			Invalidate();
		}
		if( event.Type == UIEVENT_KEYDOWN )
		{
			if (IsKeyboardEnabled()) {
				if( event.chKey == VK_SPACE || event.chKey == VK_RETURN ) {
					Activate();
					return;
				}
			}
		}
		if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )
		{
			if( ::PtInRect(&m_rcItem, event.ptMouse) && IsEnabled() ) {
				m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
				Invalidate();
			}
			return;
		}
		if( event.Type == UIEVENT_MOUSEMOVE )
		{
			if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
				if( ::PtInRect(&m_rcItem, event.ptMouse) ) m_uButtonState |= UISTATE_PUSHED;
				else m_uButtonState &= ~UISTATE_PUSHED;
				Invalidate();
			}
			return;
		}
		if( event.Type == UIEVENT_BUTTONUP )
		{
			if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
				if( ::PtInRect(&m_rcItem, event.ptMouse) ) Activate();
				m_uButtonState &= ~(UISTATE_PUSHED | UISTATE_CAPTURED);
				Invalidate();
			}
			return;
		}
		if( event.Type == UIEVENT_CONTEXTMENU )
		{
			if( IsContextMenuUsed() ) {
				m_pManager->SendNotify(this, DUI_MSGTYPE_MENU, event.wParam, event.lParam);
			}
			return;
		}
		if( event.Type == UIEVENT_MOUSEENTER )
		{
			if( IsEnabled() ) {
				m_uButtonState |= UISTATE_HOT;
				Invalidate();
			}
			// return;
		}
		if( event.Type == UIEVENT_MOUSELEAVE )
		{
			if( IsEnabled() ) {
				m_uButtonState &= ~UISTATE_HOT;
				Invalidate();
			}
			// return;
		}
		if( event.Type == UIEVENT_SETCURSOR ) {
			::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
			return;
		}
		CLabelUI::DoEvent(event);
	}

	bool CButtonUI::Activate()
	{
		if( !CControlUI::Activate() ) return false;
		if( m_pManager != NULL ) m_pManager->SendNotify(this, DUI_MSGTYPE_CLICK);
		return true;
	}

	void CButtonUI::SetEnabled(bool bEnable)
	{
		CControlUI::SetEnabled(bEnable);
		if( !IsEnabled() ) {
			m_uButtonState = 0;
		}
	}

	DWORD CButtonUI::GetPushedBkColor() const
	{
		return m_dwPushedBackColor;
	}

	void CButtonUI::SetPushedBkColor(DWORD dwBackColor)
	{
		if( m_dwPushedBackColor == dwBackColor ) return;

		m_dwPushedBackColor = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetPushedBkColor2() const
	{
		return m_dwPushedBackColor2;
	}

	void CButtonUI::SetPushedBkColor2(DWORD dwBackColor)
	{
		if( m_dwPushedBackColor2 == dwBackColor ) return;

		m_dwPushedBackColor2 = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetPushedBkColor3() const
	{
		return m_dwPushedBackColor3;
	}

	void CButtonUI::SetPushedBkColor3(DWORD dwBackColor)
	{
		if( m_dwPushedBackColor3 == dwBackColor ) return;

		m_dwPushedBackColor3 = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetHotBkColor() const
	{
		return m_dwHotBackColor;
	}

	void CButtonUI::SetHotBkColor(DWORD dwBackColor)
	{
		if( m_dwHotBackColor == dwBackColor ) return;

		m_dwHotBackColor = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetHotBkColor2() const
	{
		return m_dwHotBackColor2;
	}

	void CButtonUI::SetHotBkColor2(DWORD dwBackColor)
	{
		if( m_dwHotBackColor2 == dwBackColor ) return;

		m_dwHotBackColor2 = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetHotBkColor3() const
	{
		return m_dwHotBackColor3;
	}

	void CButtonUI::SetHotBkColor3(DWORD dwBackColor)
	{
		if( m_dwHotBackColor3 == dwBackColor ) return;

		m_dwHotBackColor3 = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetFocusedBkColor() const
	{
		return m_dwFocusedBackColor;
	}

	void CButtonUI::SetFocusedBkColor(DWORD dwBackColor)
	{
		if( m_dwFocusedBackColor == dwBackColor ) return;

		m_dwFocusedBackColor = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetFocusedBkColor2() const
	{
		return m_dwFocusedBackColor2;
	}

	void CButtonUI::SetFocusedBkColor2(DWORD dwBackColor)
	{
		if( m_dwFocusedBackColor2 == dwBackColor ) return;

		m_dwFocusedBackColor2 = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetFocusedBkColor3() const
	{
		return m_dwFocusedBackColor3;
	}

	void CButtonUI::SetFocusedBkColor3(DWORD dwBackColor)
	{
		if( m_dwFocusedBackColor3 == dwBackColor ) return;

		m_dwFocusedBackColor3 = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetFocusedHotBkColor() const
	{
		return m_dwFocusedHotBackColor;
	}

	void CButtonUI::SetFocusedHotBkColor(DWORD dwBackColor)
	{
		if( m_dwFocusedHotBackColor == dwBackColor ) return;

		m_dwFocusedHotBackColor = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetFocusedHotBkColor2() const
	{
		return m_dwFocusedHotBackColor2;
	}

	void CButtonUI::SetFocusedHotBkColor2(DWORD dwBackColor)
	{
		if( m_dwFocusedHotBackColor2 == dwBackColor ) return;

		m_dwFocusedHotBackColor2 = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetFocusedHotBkColor3() const
	{
		return m_dwFocusedHotBackColor3;
	}

	void CButtonUI::SetFocusedHotBkColor3(DWORD dwBackColor)
	{
		if( m_dwFocusedHotBackColor3 == dwBackColor ) return;

		m_dwFocusedHotBackColor3 = dwBackColor;
		Invalidate();
	}

	DWORD CButtonUI::GetHotBorderColor() const
	{
		return m_dwHotBorderColor;
	}

	void CButtonUI::SetHotBorderColor(DWORD dwBorderColor)
	{
		if( m_dwHotBorderColor == dwBorderColor ) return;

		m_dwHotBorderColor = dwBorderColor;
		Invalidate();
	}

	DWORD CButtonUI::GetPushedBorderColor() const
	{
		return m_dwPushedBorderColor;
	}
	
	void CButtonUI::SetPushedBorderColor(DWORD dwBorderColor)
	{
		if( m_dwPushedBorderColor == dwBorderColor ) return;

		m_dwPushedBorderColor = dwBorderColor;
		Invalidate();
	}

	DWORD CButtonUI::GetFocusedBorderColor() const
	{
		return m_dwFocusedBorderColor;
	}
	
	void CButtonUI::SetFocusedBorderColor(DWORD dwBorderColor)
	{
		if( m_dwFocusedBorderColor == dwBorderColor ) return;

		m_dwFocusedBorderColor = dwBorderColor;
		Invalidate();
	}

	void CButtonUI::SetHotTextColor(DWORD dwColor)
	{
		m_dwHotTextColor = dwColor;
	}

	DWORD CButtonUI::GetHotTextColor() const
	{
		return m_dwHotTextColor;
	}

	void CButtonUI::SetPushedTextColor(DWORD dwColor)
	{
		m_dwPushedTextColor = dwColor;
	}

	DWORD CButtonUI::GetPushedTextColor() const
	{
		return m_dwPushedTextColor;
	}

	void CButtonUI::SetFocusedTextColor(DWORD dwColor)
	{
		m_dwFocusedTextColor = dwColor;
	}

	DWORD CButtonUI::GetFocusedTextColor() const
	{
		return m_dwFocusedTextColor;
	}

	LPCTSTR CButtonUI::GetNormalImage()
	{
		return m_sNormalImage;
	}

	void CButtonUI::SetNormalImage(LPCTSTR pStrImage)
	{
		m_sNormalImage = pStrImage;
		Invalidate();
	}

	LPCTSTR CButtonUI::GetHotImage()
	{
		return m_sHotImage;
	}

	void CButtonUI::SetHotImage(LPCTSTR pStrImage)
	{
		m_sHotImage = pStrImage;
		Invalidate();
	}

	LPCTSTR CButtonUI::GetPushedImage()
	{
		return m_sPushedImage;
	}

	void CButtonUI::SetPushedImage(LPCTSTR pStrImage)
	{
		m_sPushedImage = pStrImage;
		Invalidate();
	}

	LPCTSTR CButtonUI::GetFocusedImage()
	{
		return m_sFocusedImage;
	}

	void CButtonUI::SetFocusedImage(LPCTSTR pStrImage)
	{
		m_sFocusedImage = pStrImage;
		Invalidate();
	}

	LPCTSTR CButtonUI::GetFocusedHotImage()
	{
		return m_sFocusedHotImage;
	}

	void CButtonUI::SetFocusedHotImage(LPCTSTR pStrImage)
	{
		m_sFocusedHotImage = pStrImage;
		Invalidate();
	}


	LPCTSTR CButtonUI::GetDisabledImage()
	{
		return m_sDisabledImage;
	}

	void CButtonUI::SetDisabledImage(LPCTSTR pStrImage)
	{
		m_sDisabledImage = pStrImage;
		Invalidate();
	}

	//************************************
	// Method:    GetForeImage
	// FullName:  CButtonUI::GetForeImage
	// Access:    public 
	// Returns:   LPCTSTR
	// Qualifier:
	// Note:	  
	//************************************
	LPCTSTR CButtonUI::GetForeImage()
	{
		return m_sForeImage;
	}

	//************************************
	// Method:    SetForeImage
	// FullName:  CButtonUI::SetForeImage
	// Access:    public 
	// Returns:   void
	// Qualifier:
	// Parameter: LPCTSTR pStrImage
	// Note:	  
	//************************************
	void CButtonUI::SetForeImage( LPCTSTR pStrImage )
	{
		m_sForeImage = pStrImage;
		Invalidate();
	}

	//************************************
	// Method:    GetHotForeImage
	// FullName:  CButtonUI::GetHotForeImage
	// Access:    public 
	// Returns:   LPCTSTR
	// Qualifier:
	// Note:	  
	//************************************
	LPCTSTR CButtonUI::GetHotForeImage()
	{
		return m_sHotForeImage;
	}

	//************************************
	// Method:    SetHotForeImage
	// FullName:  CButtonUI::SetHotForeImage
	// Access:    public 
	// Returns:   void
	// Qualifier:
	// Parameter: LPCTSTR pStrImage
	// Note:	  
	//************************************
	void CButtonUI::SetHotForeImage( LPCTSTR pStrImage )
	{
		m_sHotForeImage = pStrImage;
		Invalidate();
	}

	SIZE CButtonUI::EstimateSize(SIZE szAvailable)
	{
		if( m_cxyFixed.cy == 0 ) return CSize(m_cxyFixed.cx, m_pManager->GetFontInfo(GetFont())->tm.tmHeight + 8);
		return CControlUI::EstimateSize(szAvailable);
	}

	void CButtonUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
	{
		if( _tcscmp(pstrName, _T("normalimage")) == 0 ) SetNormalImage(pstrValue);
		else if( _tcscmp(pstrName, _T("hotimage")) == 0 ) SetHotImage(pstrValue);
		else if( _tcscmp(pstrName, _T("pushedimage")) == 0 ) SetPushedImage(pstrValue);
		else if( _tcscmp(pstrName, _T("focusedimage")) == 0 ) SetFocusedImage(pstrValue);
		else if( _tcscmp(pstrName, _T("focusedhotimage")) == 0 ) SetFocusedHotImage(pstrValue);
		else if( _tcscmp(pstrName, _T("disabledimage")) == 0 ) SetDisabledImage(pstrValue);
		else if( _tcscmp(pstrName, _T("foreimage")) == 0 ) SetForeImage(pstrValue);
		else if( _tcscmp(pstrName, _T("hotforeimage")) == 0 ) SetHotForeImage(pstrValue);
		else if( _tcscmp(pstrName, _T("pushedbkcolor")) == 0 || _tcscmp(pstrName, _T("pushedbkcolor1")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetPushedBkColor(clrColor);
		}
		else if( _tcscmp(pstrName, _T("pushedbkcolor2")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetPushedBkColor2(clrColor);
		}
		else if( _tcscmp(pstrName, _T("pushedbkcolor3")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetPushedBkColor3(clrColor);
		}
		else if( _tcscmp(pstrName, _T("hotbkcolor")) == 0 || _tcscmp(pstrName, _T("hotbkcolor1")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetHotBkColor(clrColor);
		}
		else if( _tcscmp(pstrName, _T("hotbkcolor2")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetHotBkColor2(clrColor);
		}
		else if( _tcscmp(pstrName, _T("hotbkcolor3")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetHotBkColor3(clrColor);
		}
		else if( _tcscmp(pstrName, _T("focusedbkcolor")) == 0 || _tcscmp(pstrName, _T("focusedbkcolor1")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetFocusedBkColor(clrColor);
		}
		else if( _tcscmp(pstrName, _T("focusedbkcolor2")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetFocusedBkColor2(clrColor);
		}
		else if( _tcscmp(pstrName, _T("focusedbkcolor3")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetFocusedBkColor3(clrColor);
		}
		else if( _tcscmp(pstrName, _T("focusedhotbkcolor")) == 0 || _tcscmp(pstrName, _T("focusedhotbkcolor1")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetFocusedHotBkColor(clrColor);
		}
		else if( _tcscmp(pstrName, _T("focusedhotbkcolor2")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetFocusedHotBkColor2(clrColor);
		}
		else if( _tcscmp(pstrName, _T("focusedhotbkcolor3")) == 0 ) {
			while( *pstrValue > _T('\0') && *pstrValue <= _T(' ') ) pstrValue = ::CharNext(pstrValue);
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetFocusedHotBkColor3(clrColor);
		}
		else if( _tcscmp(pstrName, _T("hotbordercolor")) == 0 ) {
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetHotBorderColor(clrColor);
		}
		else if( _tcscmp(pstrName, _T("pushedbordercolor")) == 0 ) {
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetPushedBorderColor(clrColor);
		}
		else if( _tcscmp(pstrName, _T("focusedbordercolor")) == 0 ) {
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetFocusedBorderColor(clrColor);
		}
		else if( _tcscmp(pstrName, _T("hottextcolor")) == 0 )
		{
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetHotTextColor(clrColor);
		}
		else if( _tcscmp(pstrName, _T("pushedtextcolor")) == 0 )
		{
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetPushedTextColor(clrColor);
		}
		else if( _tcscmp(pstrName, _T("focusedtextcolor")) == 0 )
		{
			if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
			LPTSTR pstr = NULL;
			DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
			SetFocusedTextColor(clrColor);
		}
		else CLabelUI::SetAttribute(pstrName, pstrValue);
	}

	void CButtonUI::PaintText(HDC hDC)
	{
		if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
		else m_uButtonState &= ~ UISTATE_FOCUSED;
		if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
		else m_uButtonState &= ~ UISTATE_DISABLED;

		if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
		if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();

		if( m_sText.IsEmpty() ) return;
		int nLinks = 0;
		RECT rc = m_rcItem;
		rc.left += m_rcTextPadding.left;
		rc.right -= m_rcTextPadding.right;
		rc.top += m_rcTextPadding.top;
		rc.bottom -= m_rcTextPadding.bottom;

		DWORD clrColor = IsEnabled()?m_dwTextColor:m_dwDisabledTextColor;

		if( ((m_uButtonState & UISTATE_PUSHED) != 0) && (GetPushedTextColor() != 0) )
			clrColor = GetPushedTextColor();
		else if( ((m_uButtonState & UISTATE_HOT) != 0) && (GetHotTextColor() != 0) )
			clrColor = GetHotTextColor();
		else if( ((m_uButtonState & UISTATE_FOCUSED) != 0) && (GetFocusedTextColor() != 0) )
			clrColor = GetFocusedTextColor();

		if( m_bShowHtml )
			CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, clrColor, \
			NULL, NULL, nLinks, m_uTextStyle);
		else
			CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, clrColor, \
			m_iFont, m_uTextStyle);
	}

	void CButtonUI::PaintStatusImage(HDC hDC)
	{
		if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
		else m_uButtonState &= ~ UISTATE_FOCUSED;
		if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
		else m_uButtonState &= ~ UISTATE_DISABLED;

		if( (m_uButtonState & UISTATE_DISABLED) != 0 ) {
			if( !m_sDisabledImage.IsEmpty() )
			{
				if( !DrawImage(hDC, (LPCTSTR)m_sDisabledImage) ) m_sDisabledImage.Empty();
				else goto Label_ForeImage;
			}
		}
		else if( (m_uButtonState & UISTATE_PUSHED) != 0 ) {
			if( !m_sPushedImage.IsEmpty() ) {
				if( !DrawImage(hDC, (LPCTSTR)m_sPushedImage) ){
					m_sPushedImage.Empty();
				}
				if( !m_sPushedForeImage.IsEmpty() )
				{
					if( !DrawImage(hDC, (LPCTSTR)m_sPushedForeImage) )
						m_sPushedForeImage.Empty();
					return;
				}
				else goto Label_ForeImage;
			}
		}
		else if( (m_uButtonState & UISTATE_HOT) != 0 && (m_uButtonState & UISTATE_FOCUSED) != 0) {
			if( !m_sFocusedHotImage.IsEmpty() ) {
				if( !DrawImage(hDC, (LPCTSTR)m_sFocusedHotImage) ) m_sFocusedHotImage.Empty();
				else goto Label_ForeImage;
			}
		}
		else if( (m_uButtonState & UISTATE_HOT) != 0 ) {
			if( !m_sHotImage.IsEmpty() ) {
				if( !DrawImage(hDC, (LPCTSTR)m_sHotImage) ){
					m_sHotImage.Empty();
				}
				if( !m_sHotForeImage.IsEmpty() ) {
					if( !DrawImage(hDC, (LPCTSTR)m_sHotForeImage) )
						m_sHotForeImage.Empty();
					return;
				}
				else goto Label_ForeImage;
			}
		}
		else if( (m_uButtonState & UISTATE_FOCUSED) != 0 ) {
			if( !m_sFocusedImage.IsEmpty() ) {
				if( !DrawImage(hDC, (LPCTSTR)m_sFocusedImage) ) m_sFocusedImage.Empty();
				else goto Label_ForeImage;
			}
		}

		if( !m_sNormalImage.IsEmpty() ) {
			if( !DrawImage(hDC, (LPCTSTR)m_sNormalImage) ) m_sNormalImage.Empty();
			else goto Label_ForeImage;
		}

		if(!m_sForeImage.IsEmpty() )
			goto Label_ForeImage;

		return;

Label_ForeImage:
		if(!m_sForeImage.IsEmpty() ) {
			if( !DrawImage(hDC, (LPCTSTR)m_sForeImage) ) m_sForeImage.Empty();
		}
	}

	void CButtonUI::PaintBkColor(HDC hDC)
	{
		if (GetPushedBkColor()!=0 || GetHotBkColor() != 0 || GetFocusedBkColor() != 0 || GetFocusedHotBkColor())
		{
			DWORD clrColor = 0,clrColor2 = 0,clrColor3 = 0;
			if ((m_uButtonState & UISTATE_PUSHED) != 0 && GetPushedBkColor()!=0){
					clrColor = GetPushedBkColor();
					clrColor2 = GetPushedBkColor2();
					clrColor3 = GetPushedBkColor3();
			}
			else if( IsFocused() && ((m_uButtonState & UISTATE_HOT) != 0) && GetFocusedHotBkColor()!=0){
				clrColor = GetFocusedHotBkColor();
				clrColor2 = GetFocusedHotBkColor2();
				clrColor3 = GetFocusedHotBkColor3();
			}
			else if( ((m_uButtonState & UISTATE_HOT) != 0) && GetHotBkColor()!=0){
					clrColor = GetHotBkColor();
					clrColor2 = GetHotBkColor2();
					clrColor3 = GetHotBkColor3();
			}
			else if ( IsFocused() && GetFocusedBkColor()!=0){
					clrColor = GetFocusedBkColor();
					clrColor2 = GetFocusedBkColor2();
					clrColor3 = GetFocusedBkColor3();
			}
			else {
				CLabelUI::PaintBkColor(hDC);
				return;
			}
			
			if( clrColor != 0 ) {
				if( clrColor2 != 0 ) {
					if( clrColor3 != 0 ) {
						RECT rc = m_rcItem;
						rc.bottom = (rc.bottom + rc.top) / 2;
						CRenderEngine::DrawGradient(hDC, rc, GetAdjustColor(clrColor), GetAdjustColor(clrColor2), true, 8);
						rc.top = rc.bottom;
						rc.bottom = m_rcItem.bottom;
						CRenderEngine::DrawGradient(hDC, rc, GetAdjustColor(clrColor2), GetAdjustColor(clrColor3), true, 8);
					}
					else
						CRenderEngine::DrawGradient(hDC, m_rcItem, GetAdjustColor(clrColor), GetAdjustColor(clrColor2), true, 16);
				}
				else if( clrColor >= 0xFF000000 ) CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(clrColor));
				else CRenderEngine::DrawColor(hDC, m_rcItem, GetAdjustColor(clrColor));
			}
           return;
		}

		CLabelUI::PaintBkColor(hDC);
	}

	void CButtonUI::PaintBorder(HDC hDC)
	{
		if (GetPushedBorderColor()!=0 || GetHotBorderColor() != 0 || GetFocusedBorderColor() != 0)
		{
			DWORD clrColor = 0;
			if ((m_uButtonState & UISTATE_PUSHED) != 0 && GetPushedBorderColor()!=0)
				clrColor = GetPushedBorderColor();
			else if( ((m_uButtonState & UISTATE_HOT) != 0) && GetHotBorderColor()!=0)
				clrColor = GetHotBorderColor();
			else if ( IsFocused() && GetFocusedBorderColor()!=0)
				clrColor = GetFocusedBorderColor();
			else 
			{
				CLabelUI::PaintBorder(hDC);
				return;
			}

			if(m_nBorderSize > 0)
			{
				if( m_cxyBorderRound.cx > 0 || m_cxyBorderRound.cy > 0 )
						CRenderEngine::DrawRoundRect(hDC, m_rcItem, m_nBorderSize, m_cxyBorderRound.cx, m_cxyBorderRound.cy, GetAdjustColor(clrColor));
				else
						CRenderEngine::DrawRect(hDC, m_rcItem, m_nBorderSize, GetAdjustColor(clrColor));
			}
			else if (m_rcBorderSize.left > 0 || m_rcBorderSize.top > 0 || m_rcBorderSize.right > 0 || m_rcBorderSize.bottom > 0)
			{
				if(m_rcBorderSize.left > 0){
						RECT rcBorder;
						rcBorder		= m_rcItem;
						rcBorder.right	= rcBorder.left;
						CRenderEngine::DrawLine(hDC,rcBorder,m_rcBorderSize.bottom,GetAdjustColor(clrColor),m_nBorderStyle);
					}
				if(m_rcBorderSize.top > 0){
						RECT rcBorder;
						rcBorder		= m_rcItem;
						rcBorder.bottom	= rcBorder.top;
						CRenderEngine::DrawLine(hDC,rcBorder,m_rcBorderSize.bottom,GetAdjustColor(clrColor),m_nBorderStyle);
					}
				if(m_rcBorderSize.right > 0){
						RECT rcBorder;
						rcBorder		= m_rcItem;
						rcBorder.right  = m_rcItem.right - 1;
						rcBorder.left	= rcBorder.right;
						CRenderEngine::DrawLine(hDC,rcBorder,m_rcBorderSize.left,GetAdjustColor(clrColor),m_nBorderStyle);
					}
				if(m_rcBorderSize.bottom > 0){
						RECT rcBorder;
						rcBorder		= m_rcItem;
						rcBorder.bottom  = m_rcItem.bottom - 1;
						rcBorder.top	= rcBorder.bottom;
						CRenderEngine::DrawLine(hDC,rcBorder,m_rcBorderSize.bottom,GetAdjustColor(clrColor),m_nBorderStyle);
					}
			}
		}

		CLabelUI::PaintBorder(hDC);
	}
}
